/*
 * OpenPanel.java
 * Describe SWF
 *
 * A browser for Flash files.
 * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * For more information please contact:
 * 
 * Stuart MacKay
 * Flagstone Software Ltd
 * 92 High Street
 * Wick, Caithness KW1 4LY
 * Scotland
 * 
 * www.flagstonesoftware.com
 */
package com.flagstone.describe;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

final class OpenPanel
{
    private final String[] fileExtension;
    private final String fileDescription;
    
    private JFileChooser chooser = new JFileChooser();
    private File selectedFile = null;
            
    OpenPanel(String[] extension, String description)
    {    
        fileExtension = extension;
        fileDescription = description;
        
        FileFilter filter = new FileFilter() 
        {
            public boolean accept(File aFile) 
            {
                boolean acceptFile = false;
                
                if (fileExtension != null)
                {
                    for (int i=0; i<fileExtension.length; i++)
                    {
                        acceptFile = acceptFile || aFile.isDirectory() || aFile.getName().endsWith(fileExtension[i]);                        
                    }
                }
                return acceptFile;
            }
            public String getDescription() 
            { 
                return fileDescription; 
            }
        };
        
        chooser.setFileFilter(filter);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    }
    
    File open()
    {
        String cwd = (String)Preferences.getInstance().get("CWD");
        selectedFile = null;
        
        if (cwd != null)
            chooser.setCurrentDirectory(new File(cwd));
        
        int option = chooser.showOpenDialog(null);
    
        switch (option)
        {
            case JFileChooser.APPROVE_OPTION:
                selectedFile = selectedFile();
                Preferences.getInstance().setUserPreference("CWD", selectedFile.getParent());
    
                if(selectedFile.isDirectory()) 
                {
                    chooser.setCurrentDirectory(selectedFile);
                    selectedFile = open();
                }
                break;
            case JFileChooser.CANCEL_OPTION:
                break;
            default:
                break;
        }
        return selectedFile;
    }     
    String saveAs()
    {
        String path = null;
        String cwd = (String)Preferences.getInstance().get("CWD");
        
        if (cwd != null)
            chooser.setCurrentDirectory(new File(cwd));
        
        int option = chooser.showSaveDialog(null);
    
        switch (option)
        {
            case JFileChooser.APPROVE_OPTION:
                selectedFile = selectedFile();
                path = selectedFile.getAbsolutePath();
    
                if(selectedFile.isDirectory()) 
                {
                    chooser.setCurrentDirectory(selectedFile);
                    Preferences.getInstance().setUserPreference("CWD", selectedFile.getPath());
                    path = saveAs();
                }
                break;
            case JFileChooser.CANCEL_OPTION:
                break;
            default:
                break;
        }
        return path;
    }
    
    private File selectedFile()
    {
        File file = chooser.getSelectedFile();
        String path = file.getAbsolutePath();
        int index = path.indexOf("~");

        if (index != -1) 
        {
            path = path.substring(index+1);
            file = new File( System.getProperty("user.home"), path);
        }
        return file;
    }
}
